var curWebview;
var paredntWebview;
var notieWebview;
var chat = {
	//连接建立时发送登录信息
	onopen : function(){
	    // 登录
	    var _opt = {
	    	"type" : "userlogin",
	    	"un" : name.replace(/"/g, '\\"'),
	    	"pw" : pass,
	    	"room_id" : into_room_id,
	    };
	    ws.send(JSON.stringify(_opt));
	},
	onmessage : function(e){
		var data = eval("("+e.data+")");
		isDebug && console.log("帐号数据："+JSON.stringify(data));
	    switch(data['type']){
	        case 'ping':
	            ws.send('{"type":"pong"}');
	            break;
	        case 'userlogin':
	        	var userinfo = data['user'];
	            if(parseInt(data['state']) != 200){
			    	mui.toast(info[data['msgid']]);
			    	return false;
			    }
	            console.log("user="+data['user']);
	            plus.storage.setItem("user",JSON.stringify(userinfo));
	            //通知更新数据
	            mui.fire(plus.webview.getWebviewById(barItemArray[2]), 'loginSuccess', userinfo);
	            mui.fire(plus.webview.getWebviewById(barItemArray[4]), 'loginSuccess', userinfo);
	            mui.fire(plus.webview.getWebviewById('room/room-redpacket.html'), 'loginSuccess', userinfo);
	            mui.back();
	            break;
	    }
	},
};
mui.init();
mui.plusReady(function(){
	plus.navigator.setStatusBarBackground("#cf2d28");
	plus.webview.currentWebview().setStyle({
		scrollIndicator: 'none',
		softinputMode: "adjustResize",
	});
	currentWebview = plus.webview.currentWebview();
	paredntWebview = currentWebview.parent();
	notieWebview = currentWebview.pWebview;
	//back时关闭窗口
	currentWebview.addEventListener('hide', function() {
		loginWebview.close();

	}, false);
	//document.addEventListener('touchstart',function(e){e.preventDefault();})
	
	mui("body").on("tap","input",function(e){
		
	});
	mui("body").on("tap","button#loginSubmit",function(e){
		var userne = document.getElementById("username");
		var passwd = document.getElementById("password");
		if(userne.value.trim()===""){
			mui.toast("请输入用户名");
			userne.focus();
			return false;
		}
		if(passwd.value.trim()===""){
			mui.toast("请输入登录密码");
			passwd.focus();
			return false;
		};
		name = userne.value;
		pass = passwd.value;
		room_connect(chat.onopen,chat.onmessage,false);
	});
	mui("body").on("tap","#register",function(e){
		mui.openWindow({
			url : "../user/register.html",
			id : "user/register.html",
			show:{                               
	            aniShow:"slide-in-right",
	            duration:300,
	            autoShow:true
	      },
		});
		//var ws=plus.webview.currentWebview();
		//plus.webview.close(ws);
	});
	mui("body").on("tap","#registerTrySubmit",function(e){
		mui.openWindow({
			url : "../user/tryregister.html",
			id : "user/tryregister.html",
			show:{                               
	            aniShow:"slide-in-right",
	            duration:300,
	            autoShow:true
	      },
		});
	});
	mui("body").on("tap","#forgetPassword",function(e){
		mui.toast('请联系客服或代理修改密码');
	});
});
